package com.dataart.training.crimequalification.dao;

import com.dataart.training.crimequalification.entity.ApplicationEnvironmentEntity;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ApplicationEnvironmentDAOImpl implements ApplicationEnvironmentDAO {

    //-----------------------------------------------------------------

    @Autowired
    private SessionFactory sessionFactory;

    // ----------------------------------------------------------------

    /**
     * Returns the application environment variable by the given key.
     *
     * @param variableName key to look up application environment variable by
     * @return string representation on the environment variable by the given name
     */
    @Override
    public String getApplicationEnvironmentVariableByName(String variableName) {
        ApplicationEnvironmentEntity environmentEntity =
                (ApplicationEnvironmentEntity) sessionFactory.getCurrentSession().
                        createCriteria(ApplicationEnvironmentEntity.class)
                        .add(Restrictions.eq("variableName", variableName)).uniqueResult();
        if (environmentEntity != null) {
            return environmentEntity.getVariableValue();
        }
        return null;
    }

}
